use std::collections::BTreeMap;
use hmac::{Hmac, Mac};
use jwt::{SignWithKey, VerifyWithKey};
use sha2::Sha256;
const JWT_SECRET: &str = "hello-world";
const TOKEN_KEY: &str = "account";
// 生成JWT签名
pub fn sign(account: &str) -> String {
    let key: Hmac<Sha256> = Hmac::new_from_slice(JWT_SECRET.as_ref()).unwrap();
    let mut claims = BTreeMap::new();
    claims.insert(TOKEN_KEY, account);
    claims.sign_with_key(&key).unwrap()
}
// 验证JWT签名
pub fn verify(token: &str, account: &str) -> bool {
    let key: Hmac<Sha256> = Hmac::new_from_slice(JWT_SECRET.as_ref()).unwrap();
    match token.verify_with_key(&key){
        Ok(claims) => {
            let claims: BTreeMap<String, String> = claims;
            claims[TOKEN_KEY] == account
        }
        Err(_) => false,
    }
}